<?php require_once(BASEPATH.'application/controllers/validaciones.php');
class formatos extends validaciones { 	
	function formatos(){
		parent::Controller(); 
		$this->load->library("rapyd");
		//$this->datasis->modulo_id(98,1);
	}
	
	function index(){
		redirect("supervisor/formatos/filteredgrid");
	}
	 
	function filteredgrid(){
		$this->rapyd->load("datafilter","datagrid");
				
		$filter = new DataFilter("Buscar y Filtrar Formatos","formatos");
						
		$filter->nombre = new inputField("Nombre", "nombre");
		$filter->nombre->db_name='nombre';
		$filter->nombre->size=20;
		
		$filter->formato = new inputField("Contenido","formato");
		$filter->formato->db_name='formato';
		$filter->formato->size=50;
		
		$filter->buttons("reset","search");
		$filter->build();
		
		$uri = anchor("supervisor/formatos/dataedit/show/<#nombre#>",'<#nombre#>');
		$uri1 = anchor('supervisor/formatos/reporte/modify/<#nombre#>/' ,'Editar');
		
		$grid = new DataGrid("");
		$grid->order_by("nombre","asc");
		$grid->per_page = 15;
		
		$grid->column_orderby("Nombre",    $uri,"nombre","align='left'NOWRAP");
		$grid->column("Formato" ,$uri1,"align='center'");
								
		$grid->add("supervisor/formatos/dataedit/create");
		$grid->build();
		//echo $grid->db->last_query();
		
		$url=site_url('supervisor/formatos/cactivo');
		$data['script']='<script type="text/javascript">
		$(document).ready(function() {
			$("form :checkbox").click(function () {
           $.ajax({
					  type: "POST",
					  url: "'.$url.'",
					  data: "codigo="+this.name,
					  success: function(msg){
					  	if (msg==0)
					    	alert("Ocurrio un problema");
					  }
					});
        }).change();
		});
		</script>';
		//$data['content'] = $filter->output.'<form>'.$grid->output.'</form>';
		$data['filtro']  = $filter->output;
		$data['content'] = $grid->output;
		$data['script'] = script("jquery.js")."\n";
		$data['title']   = heading("Formatos");
		$data["head"]    = script("jquery.pack.js").$this->rapyd->get_head();
		$this->load->view('view_ventanas', $data);	
	}
	
	function reporte(){
		$nombre=$this->uri->segment(5);
		$this->rapyd->load("dataedit");
				
		$edit = new DataEdit("Proteo", "formatos");
		$edit->back_url = site_url("supervisor/formatos/filteredgrid");
		
		$edit->formato= new textareaField("", "proteo");
		$edit->formato->rows =30;
		$edit->formato->cols=120;

		$edit->buttons("modify", "save", "undo","back");
		$edit->build();

		$data['content'] = $edit->output;
		$data['title']   = heading($nombre);
		$data["head"]    = $this->rapyd->get_head();
		$this->load->view('view_ventanas', $data);  
	}	
	function dataedit(){
		$this->rapyd->load("dataedit");

		$edit = new DataEdit("Formato", "formatos");
		$edit->back_url = site_url("supervisor/formatos/filteredgrid");
		
		$edit->nombre = new inputField("Nombre","nombre");
		$edit->nombre->rule= "strtoupper|required";
		$edit->nombre->size = 20;

		$edit->buttons("modify", "save", "undo", "delete", "back");
		$edit->build();
		
        $data['title'] = heading("Nombre de Formato");
		$data['content'] = $edit->output;           		
		$data["head"]    = $this->rapyd->get_head();
		$this->load->view('view_ventanas', $data);  
	}
	function cactivo(){
		$codigo=$this->input->post('codigo');
		if(!empty($codigo)){
			$pk=explode('|',$codigo);
			$mSQL="UPDATE intrarepo SET activo=IF(activo='S','N','S') WHERE nombre='$pk[0]' AND modulo='$pk[1]'";
			echo $this->db->simple_query($mSQL);
		}else{
			echo 0;
		}
	}

	function _post_insert($do){
		$nombre=$do->get('nombre');
		$mSQL="INSERT IGNORE INTO `reportes` (nombre) VALUES ('$nombre')";
		$this->db->simple_query($mSQL);
		logusu('formatos',"CREADO EL REPORTE $nombre");
	}
	
	function _post_delete($do){
		$nombre=$do->get('nombre');
		$mSQL="DELETE FROM `reportes` WHERE `nombre`='$nombre'";
		$this->db->simple_query($mSQL);
		logusu('formatos',"BORRADO EL REPORTE $nombre");
	}
	
	function instalar(){
		$mSQL="CREATE TABLE IF NOT EXISTS `intrarepo` (
		  `nombre` varchar(71) NOT NULL default '',
		  `modulo` varchar(10) NOT NULL default '',
		  `titulo` varchar(20) default NULL,
		  `mensaje` varchar(60) default NULL,
		  `activo` char(1) default 'S',
		  PRIMARY KEY  (`nombre`,`modulo`)
		)";
		$this->db->simple_query($mSQL);
	}
}
?>
